home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 June / SGI Freeware 1998 June.iso / dist / fw_ATxgopher.idb / usr / freeware / src / xgopher.1.3 / single.c.z / single.c
C/C++ Source or Header  |  1998-01-21  |  12KB  |  532 lines

  1. /* single.c
  2.    make and handle callbacks for inserting a single (out of context)
  3.    gopher item */
  4.  
  5.      /*---------------------------------------------------------------*/
  6.      /* Xgopher        version 1.3     08 April 1993                  */
  7.      /*                version 1.2     20 November 1992               */
  8.      /*                version 1.1     20 April 1992                  */
  9.      /*                version 1.0     04 March 1992                  */
  10.      /* X window system client for the University of Minnesota        */
  11.      /*                                Internet Gopher System.        */
  12.      /* Allan Tuchman, University of Illinois at Urbana-Champaign     */
  13.      /*                Computing and Communications Services Office   */
  14.      /* Copyright 1992, 1993 by                                       */
  15.      /*           the Board of Trustees of the University of Illinois */
  16.      /* Permission is granted to freely copy and redistribute this    */
  17.      /* software with the copyright notice intact.                    */
  18.      /*---------------------------------------------------------------*/
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <X11/Intrinsic.h>
  23. #include <X11/StringDefs.h>
  24. #include <X11/Shell.h>
  25. #include <X11/Xaw/Box.h>
  26. #include <X11/Xaw/Command.h>
  27. #include <X11/Xaw/Form.h>
  28. #include <X11/Xaw/Label.h>
  29. #include <X11/Xaw/AsciiText.h>
  30.  
  31. #include "osdep.h"
  32. #include "gopher.h"
  33. #include "item.h"
  34. #include "misc.h"
  35. #include "help.h"
  36. #include "panel.h"
  37. #include "single.h"
  38. #include "compatR4.h"
  39. #include "xglobals.h"
  40. #include "globals.h"
  41. #include "util.h"
  42. #include "gui.h"
  43.  
  44.  
  45. static Widget        topLevel, singlePanel;
  46. static Widget        giType, giName, giPath, giHost, giPort;
  47.  
  48. #define    DEFAULT_NAME    "Specially entered Gopher item"
  49.  
  50.  
  51. #define THIS_POPUP_NAME        "singleItemPopup"
  52.  
  53.                 /* default values */
  54. static popupPosResources    placement = {
  55.     /* position at the left of the main panel, 10% down */
  56.     from_main, 0, 10, justify_top_left, justify_top_left, True, True
  57.     };
  58.  
  59.  
  60.  
  61. /* createTextFieldL
  62.    create a labelled single-line text field widget pair */
  63.  
  64. static Widget
  65. createTextFieldL(name, parent, below)
  66. char    *name;
  67. Widget    parent, below;
  68. {
  69.     char    tempName[50];
  70.     Widget    theForm, theText, theLabel;
  71.     Arg    args[10];
  72.     Cardinal    n;
  73.     Dimension    w, h;
  74.  
  75.     strcpy(tempName, name);
  76.     strcat(tempName, "Form");
  77.     n = 0;
  78.     if (below != NULL) {
  79.         XtSetArg(args[n], XtNfromVert, XtParent(below));  n++;
  80.     }
  81.     theForm = XtCreateManagedWidget(tempName, formWidgetClass,
  82.             parent, args, n);
  83.  
  84.     strcpy(tempName, name);
  85.     strcat(tempName, "Label");
  86.     n = 0;
  87.     XtSetArg(args[n], XtNbottom, XawChainBottom);  n++;
  88.     XtSetArg(args[n], XtNtop, XawChainTop);  n++;
  89.     XtSetArg(args[n], XtNright, XawChainLeft);  n++;
  90.     XtSetArg(args[n], XtNleft, XawChainLeft);  n++;
  91.     theLabel = XtCreateManagedWidget(tempName, labelWidgetClass,
  92.             theForm, args, n);
  93.     getTextSize(theLabel, 10, 1, &w, &h);
  94.     n = 0;
  95.     XtSetArg(args[n], XtNwidth, w);  n++;
  96.     XtSetArg(args[n], XtNheight, h);  n++;
  97.     XtSetValues(theLabel, args, n);
  98.     
  99.     n = 0;
  100.     XtSetArg(args[n], XtNresizable, True);  n++;
  101.     XtSetArg(args[n], XtNeditType, XawtextEdit);  n++;
  102.     XtSetArg(args[n], XtNfromHoriz, theLabel);  n++;
  103.     XtSetArg(args[n], XtNbottom, XawChainBottom);  n++;
  104.     XtSetArg(args[n], XtNtop, XawChainTop);  n++;
  105.     XtSetArg(args[n], XtNright, XawChainRight);  n++;
  106.     XtSetArg(args[n], XtNleft, XawChainLeft);  n++;
  107.     theText = XtCreateManagedWidget(name, asciiTextWidgetClass,
  108.             theForm, args, n);
  109.     setTextWidgetSize(theText, 40, 1);
  110.     XtOverrideTranslations(theText, oneLineParsed);
  111.  
  112.     return theText;
  113.     
  114. }
  115.  
  116.  
  117. /* setText
  118.    set the contents of a text field to a string */
  119.  
  120. static void
  121. setText(w, value)
  122. Widget    w;
  123. String    value;
  124. {
  125.     Arg        args[2];
  126.     Cardinal    n;
  127.  
  128.     n = 0;
  129.     XtSetArg(args[n], XtNstring, value);  n++;
  130.     XtSetValues(w, args, n);
  131. }
  132.  
  133.  
  134. /* getText
  135.    get the current contents of a text field */
  136.  
  137. static char *
  138. getText(w)
  139. Widget    w;
  140. {
  141.     Arg        args[2];
  142.     Cardinal    n;
  143.     String        value;
  144.  
  145.     n = 0;
  146.     XtSetArg(args[n], XtNstring, &value);  n++;
  147.     XtGetValues(w, args, n);
  148.  
  149.     return value;
  150. }
  151.  
  152.  
  153. /* buildSingleItem
  154.    build the gopher item supplied through this panel */
  155.  
  156. static gopherItemP
  157. buildSingleItem()
  158. {
  159.     char    *type, *name, *path, *host, *value;
  160.     int    port;
  161.  
  162.  
  163.     type = getText(giType);
  164.  
  165.     name = getText(giName);
  166.     if (strcmp(name, "") == 0) {
  167.         name = DEFAULT_NAME;
  168.     }
  169.  
  170.     path = getText(giPath);
  171.  
  172.     host = getText(giHost);
  173.  
  174.     value = getText(giPort);
  175.     if ( 1 != sscanf (value, "%d", &port) ) {
  176.         port = 0;
  177.     }
  178.  
  179.     return makeItem(*type, name, path, host, port, FALSE);
  180. }
  181.  
  182.  
  183. /* processSingleItem
  184.    process the gopher item supplied through this panel */
  185.  
  186. static void
  187. processSingleItem()
  188. {
  189.     gopherItemP    gi;
  190.  
  191.     gi = buildSingleItem();
  192.  
  193.     processItem(gi);
  194.     checkButtonState(BS_changeSelected | BS_previous);
  195.  
  196.     freeItem(gi);
  197. }
  198.  
  199.  
  200. /* displaySinglePanel
  201.    pop up the single gopher item panel window */
  202.  
  203. void
  204. displaySinglePanel()
  205. {
  206.     gopherItemP    gi = getSelectedItem();
  207.  
  208.     /* if any item or bookmark is selected, prime this panel
  209.         with those fields */
  210.  
  211.     if (gi != (gopherItemP) NULL) {
  212.         char    typeStr[2];
  213.         char    portStr[8];
  214.  
  215.         typeStr[0] = gi->type;
  216.         typeStr[1] = NULLC;
  217.         sprintf (portStr, "%d", gi->port);
  218.  
  219.         setText(giType, typeStr);
  220.         setText(giName, USER_STRING(gi));
  221.         setText(giPath, vStringValue(&(gi->selector)));
  222.         setText(giHost, gi->host);
  223.         setText(giPort, portStr);
  224.     }
  225.  
  226.     positionAPopup(singlePanel, topLevel, &placement);
  227.  
  228.     XtPopup(singlePanel, XtGrabNone);
  229.  
  230.     return;
  231. }
  232.  
  233.  
  234. /* endSinglePanel
  235.    pop down the single gopher item panel window */
  236.  
  237. void
  238. endSinglePanel()
  239. {
  240.     XtPopdown(singlePanel);
  241.  
  242.     return;
  243. }
  244.  
  245.  
  246. /* doneProc
  247.    done callback for single gopher item panel */
  248.  
  249. static void
  250. doneProc(w, client_data, call_data)
  251. Widget        w;
  252. XtPointer    client_data, call_data;
  253. {
  254.  
  255.     endSinglePanel();
  256.  
  257.     processSingleItem();
  258. }
  259.  
  260.  
  261. /* cancelProc
  262.    cancel callback for single gopher item panel */
  263.  
  264. static void
  265. cancelProc(w, client_data, call_data)
  266. Widget        w;
  267. XtPointer    client_data, call_data;
  268. {
  269.     endSinglePanel();
  270. }
  271.  
  272.  
  273. /* clearProc
  274.    clear fields callback for single gopher item panel */
  275.  
  276. static void
  277. clearProc(w, client_data, call_data)
  278. Widget        w;
  279. XtPointer    client_data, call_data;
  280. {
  281.     setText(giType, "0");
  282.     setText(giName, "");
  283.     setText(giPath, "");
  284.     setText(giHost, "");
  285.     setText(giPort, "70");
  286. }
  287.  
  288.  
  289. /* singleMarkProc
  290.    mark callback for single gopher item panel */
  291.  
  292. static void
  293. singleMarkProc(w, client_data, call_data)
  294. Widget        w;
  295. XtPointer    client_data, call_data;
  296. {
  297.     gopherItemP    gi;
  298.  
  299.     gi = buildSingleItem();
  300.     markItem(gi);
  301.     unselectAllProc();
  302.     displayBookmarks();
  303.     checkButtonState(BS_unmark | BS_unmarkAll | BS_saveMarks);
  304. }
  305.  
  306.  
  307. /* singleHelpProc
  308.    help callback for single gopher item panel. */
  309.  
  310. static void
  311. singleHelpProc(w, client_data, call_data)
  312. Widget        w;
  313. XtPointer    client_data, call_data;
  314. {
  315.     char    *string;
  316.  
  317.     showHelp("single help");
  318. }
  319.  
  320.  
  321. /* doneActionProc
  322.    implement the done action */
  323.  
  324. static void
  325. doneActionProc(w, event, params, numParams)
  326. Widget        w;
  327. XEvent        *event;
  328. String        *params;
  329. Cardinal    *numParams;
  330. {
  331.     doneProc(NULL, NULL, NULL);
  332. }
  333.  
  334.  
  335. /* markActionProc
  336.    implement the mark action */
  337.  
  338. static void
  339. markActionProc(w, event, params, numParams)
  340. Widget        w;
  341. XEvent        *event;
  342. String        *params;
  343. Cardinal    *numParams;
  344. {
  345.     singleMarkProc(NULL, NULL, NULL);
  346. }
  347.  
  348.  
  349. /* cancelActionProc
  350.    implement the cancel action */
  351.  
  352. static void
  353. cancelActionProc(w, event, params, numParams)
  354. Widget        w;
  355. XEvent        *event;
  356. String        *params;
  357. Cardinal    *numParams;
  358. {
  359.     cancelProc(NULL, NULL, NULL);
  360. }
  361.  
  362.  
  363. /* clearActionProc
  364.    implement the clear action */
  365.  
  366. static void
  367. clearActionProc(w, event, params, numParams)
  368. Widget        w;
  369. XEvent        *event;
  370. String        *params;
  371. Cardinal    *numParams;
  372. {
  373.     clearProc(NULL, NULL, NULL);
  374. }
  375.  
  376.  
  377. /* helpActionProc
  378.    implement the help action */
  379.  
  380. static void
  381. helpActionProc(w, event, params, numParams)
  382. Widget        w;
  383. XEvent        *event;
  384. String        *params;
  385. Cardinal    *numParams;
  386. {
  387.     singleHelpProc(NULL, NULL, NULL);
  388. }
  389.  
  390.  
  391. /* makeSinglePanel
  392.    create the elements of the single gopher item panel */
  393.  
  394. void
  395. makeSinglePanel(top)
  396. Widget    top;
  397. {
  398.     Widget        doneButton, helpButton, cancelButton, markButton,
  399.             clearButton, singleForm, buttonBox;
  400.     Widget        itemsForm, previous;
  401.     Arg        args[10];
  402.     Cardinal    n;
  403.  
  404.     static XtActionsRec    actionsTable[] = {
  405.                 {"singleDone",   doneActionProc},
  406.                 {"singleCancel", cancelActionProc},
  407.                 {"singleClear",     clearActionProc},
  408.                 {"singleHelp",   helpActionProc},
  409.                 };
  410.  
  411.     topLevel = top;
  412.  
  413.     XtAppAddActions(appcon, actionsTable, XtNumber(actionsTable));
  414.  
  415.  
  416.     /* create single gopher item panel */
  417.  
  418.     n = 0;
  419.     XtSetArg(args[n], XtNtitle, "Special Gopher Item");  n++;
  420.     singlePanel = XtCreatePopupShell("singlePanel",
  421.             transientShellWidgetClass, topLevel, args, n);
  422.  
  423.     /* create SINGLE ITEM form */
  424.  
  425.     singleForm = XtCreateManagedWidget("singleForm", formWidgetClass,
  426.                     singlePanel, NULL, (Cardinal) 0);
  427.  
  428.  
  429.     /* create BUTTON box */
  430.  
  431.         n = 0;
  432.         XtSetArg(args[n], XtNorientation, XtorientHorizontal);  n++;
  433.         XtSetArg(args[n], XtNtop,    XawChainTop);  n++;
  434.         XtSetArg(args[n], XtNbottom,    XawChainTop);  n++;
  435.         XtSetArg(args[n], XtNleft,    XawChainLeft);  n++;
  436.         XtSetArg(args[n], XtNright,    XawChainLeft);  n++;
  437.     buttonBox = XtCreateManagedWidget("buttonBox", boxWidgetClass,
  438.                     singleForm, args, n);
  439.  
  440.  
  441.  
  442.     /* create ITEMS form */
  443.  
  444.         n = 0;
  445.         XtSetArg(args[n], XtNfromVert,    buttonBox);  n++;
  446.         XtSetArg(args[n], XtNtop,    XawChainTop);  n++;
  447.         XtSetArg(args[n], XtNbottom,    XawChainBottom);  n++;
  448.         XtSetArg(args[n], XtNleft,    XawChainLeft);  n++;
  449.         XtSetArg(args[n], XtNright,    XawChainRight);  n++;
  450.     itemsForm = XtCreateManagedWidget("itemsForm", formWidgetClass,
  451.                     singleForm, args, n);
  452.  
  453.  
  454.     /* create text fields */
  455.  
  456.     giType = createTextFieldL("giType", itemsForm, NULL);
  457.     giName = createTextFieldL("giName", itemsForm, giType);
  458.     giPath = createTextFieldL("giPath", itemsForm, giName);
  459.     giHost = createTextFieldL("giHost", itemsForm, giPath);
  460.     giPort = createTextFieldL("giPort", itemsForm, giHost);
  461.  
  462.     setText(giType, "0");
  463.     setText(giName, "");
  464.     setText(giPath, "");
  465.     setText(giHost, "");
  466.     setText(giPort, "70");
  467.                 
  468.  
  469.     /* create QUIT button */
  470.  
  471.         n = 0;
  472.     doneButton = XtCreateManagedWidget("done", commandWidgetClass,
  473.                     buttonBox, args, n);
  474.         XtAddCallback(doneButton, XtNcallback, doneProc, NULL);
  475.  
  476.     /* create CANCEL button */
  477.  
  478.         n = 0;
  479.         XtSetArg(args[n], XtNfromHoriz,    doneButton);  n++;
  480.     cancelButton = XtCreateManagedWidget("cancel", commandWidgetClass,
  481.                     buttonBox, args, n);
  482.         XtAddCallback(cancelButton, XtNcallback, cancelProc, NULL);
  483.  
  484.     /* create CLEAR button */
  485.  
  486.         n = 0;
  487.         XtSetArg(args[n], XtNfromHoriz,    cancelButton);  n++;
  488.     clearButton = XtCreateManagedWidget("clear", commandWidgetClass,
  489.                     buttonBox, args, n);
  490.         XtAddCallback(clearButton, XtNcallback, clearProc, NULL);
  491.  
  492.     /* create MARK button */
  493.  
  494.         n = 0;
  495.         XtSetArg(args[n], XtNfromHoriz,    clearButton);  n++;
  496.     markButton = XtCreateManagedWidget("mark", commandWidgetClass,
  497.                     buttonBox, args, n);
  498.         XtAddCallback(markButton, XtNcallback, singleMarkProc, NULL);
  499.  
  500.     /* create HELP button */
  501.  
  502.         n = 0;
  503.         XtSetArg(args[n], XtNfromHoriz,    markButton);  n++;
  504.     helpButton = XtCreateManagedWidget("help", commandWidgetClass,
  505.                     buttonBox, args, n);
  506.         XtAddCallback(helpButton, XtNcallback, singleHelpProc, NULL);
  507.  
  508.  
  509.     /* for ICCCM window manager protocol complience */
  510.  
  511.     XtOverrideTranslations (singlePanel,
  512.         XtParseTranslationTable ("<Message>WM_PROTOCOLS: singleCancel()"));
  513.     XtRealizeWidget(singlePanel);
  514.     (void) XSetWMProtocols (XtDisplay(singlePanel), XtWindow(singlePanel),
  515.                                 &wmDeleteAtom, 1);
  516.  
  517.  
  518.     /* find the popup placement for this shell */
  519.  
  520.     {
  521.     popupPosResources *resourcePlacement;
  522.  
  523.     resourcePlacement = getPopupPosResources(
  524.                 THIS_POPUP_NAME, POPUP_POS_CLASS, &placement);
  525.     bcopy( (char *) resourcePlacement, (char *) &placement,
  526.                 sizeof(popupPosResources) );
  527.     }
  528.  
  529.  
  530.     return;
  531. }
  532.